//
//  SyUserInfomation.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/5/17.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "SyUserInfomation.h"

@implementation SyUserInfomation
//存储用户信息到本地
+ (BOOL)personalImformationSaveWtih:(NSDictionary *)imformation{
    if (![imformation count] )
        return NO;
    
    NSDictionary * userinfo = [imformation objectForKey:@"userinfo"];
    NSMutableDictionary *informationM = [NSMutableDictionary dictionaryWithDictionary:imformation];
    NSMutableDictionary *userinfoM = [NSMutableDictionary dictionaryWithDictionary:userinfo];
    [userinfoM setObject:[NSString stringWithFormat:@"%@",[imformation objectForKey:@"grader"]] forKey:@"grader"];
    [userinfoM setObject:[NSString stringWithFormat:@"%@",[imformation objectForKey:@"edtiontype"]] forKey:@"edtiontype"];
    [userinfoM setObject:[imformation objectForKey:@"edtiontypename"] forKey:@"edtiontypename"];
    [userinfoM setObject:[imformation objectForKey:@"gradername"] forKey:@"gradername"];
    
    [userinfoM setObject:[NSString stringWithFormat:@"%@",[imformation objectForKey:@"ishavecardid"]] forKey:@"ishavecardid"];
    [userinfoM setObject:[NSString stringWithFormat:@"%@",[imformation objectForKey:@"isvip"]] forKey:@"isvip"];
    
    NSArray *valueArr=userinfo.allValues;
    NSArray *keyArr=userinfo.allKeys;
    for ( int i=0; i<[valueArr count]; i++) {
        if ([[valueArr objectAtIndex:i] isKindOfClass:[NSNull class]]) {
            NSString *str=@"abc";
            [userinfoM setObject:str forKey:[keyArr objectAtIndex:i]];
        }
    }
    [informationM setObject:userinfoM forKey:@"userinfo"];
    NSString * userID = [NSString stringWithFormat:@"%@",[userinfo objectForKey:@"userid"]];
    if (userID.length != 0) {
        NSString * userIMFPath = [NSString stringWithFormat:@"%@/%@.plist",PERSONALIMFORMATIONPATH,userID];
        NSLog(@"sanbox path :%@",userIMFPath);
        BOOL yes = [informationM writeToFile:userIMFPath atomically:NO];
        return yes;
    }
    return NO;
}
+ (long long)getUserId
{
    //获取本地userid
    NSString *userids = [[NSUserDefaults standardUserDefaults] objectForKey:@"userid"];
    
    long long userid=0;
    @try {
        userid=[userids longLongValue];
        if (userid<0) {
            userid=0;
        }
    }
    @catch (NSException *exception) {
        userid=0;//USERID不能转换成长整型和返回null和空字符串时调用
    }
    @finally {
        
    }
    
    return userid;
}
+ (int)getLastType
{
    int count=0;
    int type=0;
    long long userid=[self getUserId];
    if (userid>0) {
        NSString *uid=[NSString stringWithFormat:@"%lld",userid];
        NSDictionary *userInfoDict=[self acquirePersonalImformationFromMemoryWithPersonID:uid];
        NSDictionary *info=[userInfoDict objectForKey:@"userinfo"];
        @try {
            type=[[info objectForKey:@"lasttype"] intValue];
        }
        @catch (NSException *exception) {
            type=0;
            
        }
        @finally {
            
        }
        
    }
    if (type==0) {
        
        if (count==0) {
            [self getUserInfoByID:[NSString stringWithFormat:@"%lld",userid]];
        }
        count++;
    }
    return type;
}
+ (NSDictionary *)acquirePersonalImformationFromMemoryWithPersonID:(NSString *)personalID{
    
    NSString * userIMFPath = [NSString stringWithFormat:@"%@/%@.plist",PERSONALIMFORMATIONPATH,personalID];
    NSFileManager * manager = [NSFileManager defaultManager];
    NSDictionary * dict = nil;
    
    if ([manager fileExistsAtPath:userIMFPath]) {
        dict = [[NSDictionary alloc] initWithContentsOfFile:userIMFPath];
    }

    return dict;
}

+(void)getUserInfoByID:(NSString *)userID
{
//    NSMutableDictionary *paramdic = [NSMutableDictionary dictionary];
//    [paramdic setObject:@"api_user_getinfobyid" forKey:@"Method"];
//    [paramdic setObject:userID forKey:@"userid"];
//    [Utils get:nil parameters:paramdic success:^(AFHTTPRequestOperation *operation, id jsonObject) {
//        
//        NSMutableDictionary *infomation = [jsonObject objectForKey:@"re_data"];
//        [self personalImformationSaveWtih:infomation];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
    
}
@end
